package com.maimang.platform.dsp.job;

import com.maimang.platform.common.entity.SysDictData;
import com.maimang.platform.common.utils.RedisUtils;
import com.maimang.platform.dsp.service.AdvertiserDataService;
import com.maimang.platform.dsp.service.AdvertiserService;
import com.maimang.platform.dsp.service.DataSyncService;
import com.maimang.platform.lang.StringUtils;
import com.maimang.platform.service.DictService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author Rocky
 * @version 1.0.0
 * @classname AdvertiserTask
 * @description <br>广告主信息定时任务</br>
 * @date 2019-08-23 16:52
 * @email jintaozhao@qq.com
 */
@Component("advertiserTask")
public class AdvertiserTask {

	private static Logger logger = LoggerFactory.getLogger(AdvertiserTask.class);

	@Autowired
	private AdvertiserService advertiserService;

	@Autowired
	private AdvertiserDataService advertiserDataService;

	@Autowired
	private DataSyncService dataSyncService;

	@Autowired
	private DictService dictService;

	private String DICT_TYPE = "dsp_agent_info";

	/**
	 * 获取广告主id列表
	 */
	public void getIds() throws InterruptedException {
		// 同步广告主id
		advertiserService.getAdvertiserIds();
	}

	/**
	 * 同步广告组和广告计划信息
	 *
	 */
	public void syncAll() {
		try {
			List<SysDictData> dictDataList = dictService.getType(DICT_TYPE);
			for (int i = 0; i < dictDataList.size(); i++) {
				SysDictData dictData = dictDataList.get(i);
				if (StringUtils.isNotNull(dictData) && StringUtils.isNotEmpty(dictData.getDictValue())) {
					String advertiserId = dictData.getDictValue();
					// 同步数据
					dataSyncService.syncData(advertiserId, 1);
				}
			}
		} catch (Exception e) {
			logger.error("同步广告组或广告计划相关数据出错", e.getMessage());
		}
	}

	/**
	 * 同步代理商下属所有广告主信息、账户资金数据等
	 *
	 */
	public void syncBase() {
		try {
			List<SysDictData> dictDataList = dictService.getType(DICT_TYPE);
			for (int i = 0; i < dictDataList.size(); i++) {
				SysDictData dictData = dictDataList.get(i);
				if (StringUtils.isNotNull(dictData) && StringUtils.isNotEmpty(dictData.getDictValue())) {
					String advertiserId = dictData.getDictValue();
					// 同步数据
					dataSyncService.syncBaseInfo(advertiserId);
				}
			}
		} catch (Exception e) {
			logger.error("同步广告主信息、账户资金数据时出错", e.getMessage());
		}
	}

	/**
	 * 同步广告组和广告计划信息
	 *
	 */
	public void syncAdvertiser() {
		try {
			List<SysDictData> dictDataList = dictService.getType(DICT_TYPE);
			for (int i = 0; i < dictDataList.size(); i++) {
				SysDictData dictData = dictDataList.get(i);
				if (StringUtils.isNotNull(dictData) && StringUtils.isNotEmpty(dictData.getDictValue())) {
					String advertiserId = dictData.getDictValue();
					// 同步数据
					dataSyncService.syncData(advertiserId, 1);
				}
			}
		} catch (Exception e) {
			logger.error("同步广告组或广告计划相关数据出错", e.getMessage());
		}
	}

	/**
	 * @param days
	 */
	public void syncDataByDate(String days) {
		try {
			//	同步最近30天广告主数据
			int dayNum = Integer.parseInt(days);
			List<SysDictData> dictDataList = dictService.getType(DICT_TYPE);
			for (int i = 0; i < dictDataList.size(); i++) {
				SysDictData dictData = dictDataList.get(i);
				if (StringUtils.isNotNull(dictData) && StringUtils.isNotEmpty(dictData.getDictValue())) {
					String advertiserId = dictData.getDictValue();
					// 同步数据
					dataSyncService.syncData(advertiserId, dayNum);
				}
			}
		} catch (Exception e) {
			logger.error("同步广告主历史资金数据出错", e.getMessage());
		}
	}

	/**
	 * 同步历史数据
	 */
	public void syncHistoryData() {
		try {
			List<SysDictData> dictDataList = dictService.getType(DICT_TYPE);
			for (int i = 0; i < dictDataList.size(); i++) {
				SysDictData dictData = dictDataList.get(i);
				if (StringUtils.isNotNull(dictData) && StringUtils.isNotEmpty(dictData.getDictValue())) {
					String advertiserId = dictData.getDictValue();
					//	同步最近30天广告主数据
					advertiserDataService.syncAdvertiserData(Long.parseLong(advertiserId), 30);
				}
			}
		} catch (Exception e) {
			logger.error("同步广告主数据出错", e.getMessage());
		}
	}

	/**
	 * @param days
	 */
	public void syncByDate(String days) {
		try {
			//	同步最近30天广告主数据
			int dayNum = Integer.parseInt(days);
			List<SysDictData> dictDataList = dictService.getType(DICT_TYPE);
			for (int i = 0; i < dictDataList.size(); i++) {
				SysDictData dictData = dictDataList.get(i);
				if (StringUtils.isNotNull(dictData) && StringUtils.isNotEmpty(dictData.getDictValue())) {
					String advertiserId = dictData.getDictValue();
					//	同步最近30天广告主数据
					advertiserDataService.syncAdvertiserData(Long.parseLong(advertiserId), dayNum);
				}
			}
		} catch (Exception e) {
			logger.error("同步广告主历史数据出错", e.getMessage());
		}
	}

}
